require 'rubygems'
require 'sprout'
require 'task/swfmill'
require 'task/mtasc'
require 'task/flash_player'
include PatternPark

############################################
# Uncomment and modify any of the following:
model = ProjectModel.instance
model.project_name  = 'LBiUseful'

model.skin_dir      = File.join(model.asset_dir, 'img', 'skins')

SWFMill.new(:compile_skin) do |t|
  t.input = model.skin_dir + '/default'
  t.template = model.skin_dir + '/SWFMillTemplate.erb'
  t.output = model.skin_dir + '/LBiUsefulSkin.swf'
end

############################################
# Default task here is the test suites.

task :default => [:test]

############################################
# Launch the Test Suites using Flash Player

task :test => [:compile_tests]

FlashPlayer.new(:test) do |t|
  t.version = 9
  t.swf = model.bin_dir + '/LBiUsefulRunner.swf'
end

MTASC.new(:compile_tests) do |t|
  t.input = 'LBiUsefulRunner'
  t.version = 8
  t.frame = 2
  t.main = true
  t.swf = model.skin_dir + '/LBiUsefulSkin.swf'
  t.out = model.bin_dir + '/LBiUsefulRunner.swf'
  t.cp << model.src_dir
  t.cp << model.test_dir
  Dir[model.lib_dir + '/**'].each do |path|
    t.cp << path
  end
end


############################################
# Generate documentation
task :docs do 
  ruby "-Cscript/as2api/ as2api.rb --classpath '#{File.expand_path('src')}' com.lbi.* --output-dir '#{File.expand_path '../doc/api'}'"
end

############################################
# Compile example application
task :example => [:compile_main]

MTASC.new(:compile_main) do |t|
  t.input = 'LBiUseful'
  t.main = true
  t.version = 8
  t.frame = 2
  t.swf = model.skin_dir + '/LBiUsefulSkin.swf'
  t.out = model.bin_dir + '/example.swf'
  t.cp << model.src_dir
  Dir[model.lib_dir + '/**'].each do |path|
    if(!path.index('unit')) # don't add unit test paths to main application
      t.cp << path
    end
  end
end

FlashPlayer.new(:example) do |t|
  t.version = 9
  t.swf = model.bin_dir + '/example.swf'
end

############################################
# export the bin dir using svn to a temp dir
# and then zip it up

task :release do
      releases_dir = "../releases" 
      mkdir releases_dir unless File.exists?(releases_dir)
      datestamp = DateTime::now().strftime("%Y-%m-%d-%H%M%S")
      temp = "#{releases_dir}/temp"
      zip_name = "#{model.project_name}_#{datestamp}.zip"
      begin
            system "svn export #{model.bin_dir} #{temp}"    
            new_zip_file = "#{releases_dir}/#{zip_name}"
            Zip::ZipFile.open(new_zip_file, Zip::ZipFile::CREATE)do |zipfile|
              Find.find(temp) do |path|
                Find.prune if File.basename(path)[0] == ?.
                dest = /#{temp}\/(\w.*)/.match(path)
                zipfile.add(dest[1],path) if dest
              end 
            end
            puts "created #{zip_name}"
      ensure
            rm_rf temp
      end
end
